﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using DTO;

namespace DataAccessTier
{
    public class LoaiHangHoaDAO:DBConnection
    {
        public LoaiHangHoaDAO() : base() { }

        public DataTable GetAllLoaiHangHoa()
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "SELECT * FROM LOAIHANGHOA";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                return dt;
            }
            catch (Exception)
            {
                conn.Close();
            }
            return null;
        }

        public bool kiemtratontai(string maloaihh)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            bool tatkt = false;
            SqlDataAdapter da_kiemtra = new SqlDataAdapter("SELECT * FROM LOAIHANGHOA WHERE MALOAI = '" + maloaihh + "'", conn);
            DataTable dt_kiemtra = new DataTable();
            da_kiemtra.Fill(dt_kiemtra);
            if (dt_kiemtra.Rows.Count > 0)
            {
                tatkt = true;
            }
            da_kiemtra.Dispose();
            conn.Close();
            return tatkt;
        }

        public bool Insert(LoaiHangHoa loaihh)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "INSERT INTO LOAIHANGHOA(MALOAI,TENLOAI) VALUES('" + loaihh.MALOAI + "',N'" + loaihh.TENLOAI + "')";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }

        public bool Delete(string maloaihh)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "DELETE FROM LOAIHANGHOA WHERE MALOAI = '" + maloaihh + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }

        public bool kiemtratontaiHH(string maloaihh)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            bool tatkt = false;
            SqlDataAdapter da_kiemtra = new SqlDataAdapter("SELECT * FROM HANGHOA WHERE MALOAI = '" + maloaihh + "'", conn);
            DataTable dt_kiemtra = new DataTable();
            da_kiemtra.Fill(dt_kiemtra);
            if (dt_kiemtra.Rows.Count > 0)
            {
                tatkt = true;
            }
            da_kiemtra.Dispose();
            conn.Close();
            return tatkt;
        }

        public bool DeleteHH(string maloaihh)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "DELETE FROM HANGHOA WHERE MALOAI = '" + maloaihh + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }

        public bool Update(LoaiHangHoa loaihh)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "UPDATE LOAIHANGHOA SET MALOAI = '" + loaihh.MALOAI + "', TENLOAI = N'" + loaihh.TENLOAI + "' WHERE MALOAI = '" + loaihh.MALOAI + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }
    }
}
